﻿namespace Beetle
{
    using System;
    using System.Collections.Generic;
    using System.Net;

    public class DefaultIPFilter : IIPFiler
    {
        private List<IpItem> _field_a = new List<IpItem>();

        public void Add(IpItem item)
        {
            lock (this._field_a)
            {
                this._field_a.Add(item);
            }
        }

        public void Add(string ipvalue, bool permit)
        {
            IpItem item = IpItem.From(ipvalue, permit);
            if (item != null)
            {
                this.Add(item);
            }
        }

        public bool Execute(IPEndPoint point)
        {
            bool flag = false;
            if (this.GetItems().Length == 0)
            {
                return true;
            }
            foreach (IpItem item in this.GetItems())
            {
                flag = item.Verification(point.Address);
                if (!flag)
                {
                    return flag;
                }
            }
            return flag;
        }

        public IpItem[] GetItems()
        {
            lock (this._field_a)
            {
                IpItem[] array = new IpItem[this._field_a.Count];
                this._field_a.CopyTo(array, 0);
                return array;
            }
        }

        public void Remove(string ipvalue, bool permit)
        {
            lock (this._field_a)
            {
                IList<IpItem> list = new List<IpItem>();
                foreach (IpItem item in this._field_a)
                {
                    if ((item.AddressValue == ipvalue) && (item.Permit == permit))
                    {
                        list.Add(item);
                    }
                }
                foreach (IpItem item2 in list)
                {
                    this._field_a.Remove(item2);
                }
            }
        }
    }
}

